/*
 * TITLE:ConnectServer
 * AUTHOR:Manori Thakur
 * PURPOSE: Communicates directly with game server
 */
package clientcore;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;

import com.example.android.debuggermenu.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class ConnectServer {
	private static final String TAG = ConnectServer.class.getSimpleName();
    private Socket gameClient = null;
    private InetAddress serverAddr = null;
    private String hostname = null;
    private int serverPort = 0;
    private AndroidMain ac = null;
    private DataOutputStream sendServer;
    private DataInputStream receiveServer;


    public ConnectServer(String hostname, int serverPort,AndroidMain ac) {
        this.hostname = hostname;
        this.serverPort = serverPort;
        this.ac = ac;
    }

    /**
     * Creates the android client TCP socket
     * @param hostname
     * @param serverPort
     */
    public boolean connectServer() {
    	try {
    		serverAddr  = InetAddress.getByName(hostname);
    	    SocketAddress serversockaddr = new InetSocketAddress(serverAddr, serverPort);

    	    // Create an unbound socket
    	    gameClient= new Socket();

    	    // This method will block no more than timeoutMs.
    	    // If the timeout occurs, SocketTimeoutException is thrown.
    	    int timeoutMs = 3000;   // 2 seconds
    	    gameClient.connect(serversockaddr, timeoutMs);
    	    //gameClient = new Socket(serverAddr,serverPort);
    	    //gameClient.setSoTimeout(3000);
    	    if(gameClient!=null) {
                sendServer = new DataOutputStream((gameClient.getOutputStream()));
    			receiveServer = new DataInputStream(gameClient.getInputStream());
    	    }
    	} catch (UnknownHostException e) {
    		Log.e(TAG,"UnknownHostException in method connectServer()"+e.toString());
    		return false;
    	} catch (SocketTimeoutException e) {
    		Log.e(TAG,"SocketTimeoutException in method connectServer()"+e.toString());
    		return false;
    	} catch (IOException e) {
    		Log.e(TAG,"IO Exception in method connectServer()"+e.toString());
    		return false;
    	}
    	return true;
    	/**
        try {
            serverAddr = InetAddress.getByName(hostname);
            gameClient = new Socket(serverAddr,serverPort);
            if(gameClient!=null) {
	            sendServer = new DataOutputStream((gameClient.getOutputStream()));
	            receiveServer = new DataInputStream(gameClient.getInputStream());
            }
            
       }
       catch(IOException ex) {
            	Log.e(TAG,"IO Exception1 in method connectServer()"+ex.toString());
       }
       catch (Exception ex) {
            	Log.e(TAG,"Exception in method connectServer()"+ex.toString());
       }**/
            
    }
    
    public DataOutputStream getDataOutputStream() {
        return sendServer;
    }

    public DataInputStream getDataInputStream() {
       return receiveServer;
    }
    
    public void flushOutputStream() {
        try {
            sendServer.flush();
        } catch (IOException ex) {
        	Log.e(TAG,"IO Exception in method flushOutputStream()"+ex.toString());
        }
    }
    
    public void closeServer() {
    	try {    		
            sendServer.close();
            receiveServer.close();
            gameClient.close();
        } catch (IOException ex) {
        	Log.e(TAG,"IO Exception in method closeServer()"+ex.toString());
        }
    }
}
